package gittexn.controller;



import cn.hutool.core.util.XmlUtil;
import cn.hutool.extra.qrcode.QrCodeUtil;
import gittexn.utils.WeChatPay;
import jakarta.servlet.ServletOutputStream;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;



import java.util.Map;

//@RestController
//@EnableAutoConfiguration
@Controller
//@RequestMapping("/cn")
public class textController122 {

    @GetMapping("co1" )
    @ResponseBody
    private void code1(HttpServletRequest request, HttpServletResponse response) throws Exception {
        // 支付的商品名称
        System.out.println("111");
    }

    @GetMapping("/1")
    @ResponseBody
    public String home() {
        return "Hello, World!";
    }

    @RequestMapping("/2")
    @ResponseBody
    public String home2() {
        return "Hello, World22!";
    }

    //预支付接口（生成二维码并输出到浏览器）
    @GetMapping("codePay" )
    @ResponseBody
    private void codePay(HttpServletRequest request, HttpServletResponse response) throws Exception {
        // 支付的商品名称
        String body="可口可乐";
        //支付金额（单位：分）
        String totalFee="1";

        //获取二维码内容urlCode
        String resXml = WeChatPay.getNative( body, totalFee);
        Map<String, Object> data= XmlUtil.xmlToMap(resXml);
        String urlCode = data.get("code_url").toString();

        //生成二维码到输出流
        response.setContentType("image/jpeg");
        ServletOutputStream out = response.getOutputStream();
        QrCodeUtil.generate(urlCode, 300, 300,"jpg" ,out);
        out.close();
    }

//    ## 回调接口
    @RequestMapping("wxnotify" )
    public void wxnotify(HttpServletRequest request, HttpServletResponse response) throws Exception {
        System.err.println("开始回调。。。");
        WeChatPay.notify(request, response);
    }

    @RequestMapping("wx1" )
    public void wxno1(HttpServletRequest request, HttpServletResponse response) throws Exception {
        System.err.println("测试。。。");
        WeChatPay.notify(request, response);
    }



}
